//
//  SinaMainBarController.swift
//  Sina
//
//  Created by 星仔 on 2017/2/23.
//  Copyright © 2017年 xingZai. All rights reserved.
//

import UIKit

class SinaMainBarController: UITabBarController {
//懒加载
    private lazy  var centerButton:UIButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildViewController()
        setupCenterButton()
    }
    //MARK:设置屏幕方向,
    //portrait 肖像，竖屏
    //landscape 风景，横屏
    //当父视图处理好时，子视图自动遵守父类的方法
    //当横屏的时候视频播放，需要通过modal来处理
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    //MARK:设置中心按钮
    @objc private func setupCenterButton (){
        tabBar.addSubview(centerButton)
        
        //容错点，将宽度减少1，2个点
        let count = viewControllers?.count
        if count == nil {
            return
        }
        let w = tabBar.bounds.width/CGFloat(count!)-1
        
        centerButton.frame = CGRect(x: 2*w, y: 0, width: w, height: tabBar.bounds.size.height)
        
        guard let image = UIImage(named: "tabbar_compose_icon_add"),
            let backImage = UIImage(named: "tabbar_compose_button") else{
                return
        }
        centerButton.setImage(image, for: .normal)
        centerButton.setBackgroundImage(backImage, for: .normal)
        centerButton.addTarget(self, action:#selector(centerButtonClick) , for: .touchUpInside)
    }
}
//分类
extension SinaMainBarController{
    //MARK:添加中心按钮
    func centerButtonClick(){
        print("中心按钮点击")
    }
    
  
    //MARK:添加子控制器
    //反射机制，将控制器的属性添加到字典数组中
    //根据命名空间+"."+"类名"反射出类
    //再设置类的title,image,文字大小 
    //再创建导航将根控制器添加上去，返回导航

    
    
    func setupChildViewController (){
        
        //从网络下载的本地的读取json/读取数据
        //如果数据为空读取本地文件
//        let urldir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
//        let dataPath = (urldir as NSString).appendingPathComponent("main.json")
//        
//        var datas = NSData(contentsOfFile: dataPath)
//        
//        if datas == nil{
//            
//            datas = NSData(contentsOfFile: Bundle.main.path(forResource: "addjson.json", ofType:nil)!)
//        }
//        
//        guard let array = try? JSONSerialization.jsonObject(with: datas! as Data, options: []) as? [[String:AnyObject]] else{
//            return
//        }
        
        let array = [["clsName":"SinaHomeViewController","title":"首页","imageName":"tabbar_home","visitInfo":["imageName":"","message":"哈哈哈哈"]],["clsName":"SinaMsgController","title":"信息","imageName":"tabbar_message_center","visitInfo":["imageName":"visitordiscover_image_message","message":"2哈哈哈哈"]],
             ["clsName":"","title":"","imageName":""],
            ["clsName":"SinaDisCorverController","title":"发现","imageName":"tabbar_discover","visitInfo":["imageName":"visitordiscover_image_message","message":"哈3哈哈哈"]],["clsName":"SinaProfileViewController","title":"我","imageName":"tabbar_profile","visitInfo":["imageName":"visitordiscover_image_profile","message":"哈4哈哈哈"]]]
        
        var arrayVc = [UIViewController]()
        for dict in array{
            arrayVc.append(addViewController(dict: dict as [String : AnyObject] ))
        }
        viewControllers = arrayVc
        
    }
    
   func addViewController(dict:[String:AnyObject])-> UIViewController{
        //判断字典中是否有title,类名，imageName
        guard let clsName = dict["clsName"] as? String,
             let title = dict["title"] as? String,
             let imageName = dict["imageName"] as? String,
             let cls = NSClassFromString(Bundle.main.nameSpace + "." + clsName) as? SinaBaseController.Type,
            let visits:[String:String] = dict["visitInfo"] as? [String:String]
            else{
                return UIViewController()
        }
        let vc = cls.init()
        vc.title = title
        vc.visitDict = visits
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.black], for: .normal)
        let nav = SinaNavController(rootViewController: vc)
        return nav
    }
}
